package org.dna.opdracht1;

import static org.dna.opdracht1.Apl.NANO;
import static org.dna.opdracht1.Apl.MORE_VERBOSE;
import java.util.HashSet;

public class RandomFrame {

	public void TestAlgorithm(RandomPerm random, int loops, int... size) {
		System.out.printf("Starting Algorithm [%s]\n", random.getName());
		for (int arraySize = 0; arraySize < size.length; arraySize++) {
			long startTime, endTime, totalTime;
			long[] times = new long[loops + 1];
			startTime = System.nanoTime();
			for (int loop = loops; loop > 0; --loop) {
				@SuppressWarnings("unused")
				int[] array = random.randomInt(size[arraySize]);
				times[loop] = random.getTotalRunningTime();
			}
			printTimes(times, size[arraySize]);
			endTime = System.nanoTime();
			totalTime = (endTime - startTime);
			System.out.printf("\tTotal Running Time For [%s] [%d]: %d ms\n",
					random.getName(), size[arraySize], totalTime / NANO);
			printAverageTimes(times, size[arraySize]);
		}
	}

	public void printTimes(long[] array, int size) {
		if (MORE_VERBOSE) {
			for (int i = array.length - 1; i > 0; i--) {
				System.out.printf("\tRuntime[%d] [%d] = %d ms\n", i / NANO,
						size, array[i]);
			}
		}
	}

	public void printAverageTimes(long[] array, int size) {
		long averageTime = 0x00;
		for (int i = array.length - 1; i > 0; i--) {
			averageTime += array[i];
		}
		averageTime /= (array.length - 1);
		System.out.printf("\t\tAverage Runtime [%d] = %d ms\n", size,
				averageTime);
	}

	public boolean hasDuplicates(int[] array) {
		HashSet<Integer> set = new HashSet<Integer>();
		for (int i = 0; i < array.length; i++) {
			if (set.contains(array[i])) {
				return true;
			} else {
				set.add(array[i]);
			}
		}
		return false;
	}

	public boolean arrayContains(int[] array, int[] contains) {
		for (int i = 0; i < array.length; i++) {
			boolean has = false;
			for (int z = 0; z < contains.length; z++) {
				if (array[i] == contains[z]) {
					has = true;
				}
			}
			if (!has) {
				return false;
			}
		}
		return true;
	}

	public boolean isSorted(int[] array) {
		boolean isSorted = true;
		int prev = array[0];
		for (int i = 1; i < array.length; i++) {
			if (array[i] < prev) {
				return false;
			}
			prev = array[i];
		}
		return isSorted;
	}
}
